import { GameData, Levels } from "../Data/GameData";
import AssetsManager from "../Manager/AssetsManager";
import CommonFunction from "../utils/CommonFunction";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainPanel extends cc.Component {
    public static Instance: MainPanel;

    @property(cc.Node)
    public Levels: cc.Node = null;

    onLoad() {
        MainPanel.Instance = this;
        // this.init();
        // this.ShowOn();
    }

    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();

        // console.log(`MainPanel-ShowOn------------`);
    }

    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }

    async Refresh() {
        CommonFunction.SetAllChildrenAtive(this.Levels, false);

        // for (let index = 0; index < AssetsManager.Levels.length; index++) {
        for (let index = 0; index < Levels.length; index++) {
            // const item_data = Levels[index];
            // console.log(`item_data`, item_data);

            const child = CommonFunction.GetFreeChild(this.Levels);
            child.active = true;
            let label_level: cc.Label = cc.find(`bg/level`, child).getComponent(cc.Label);
            // let label_info: cc.Label = cc.find(`bg/info`, child).getComponent(cc.Label);
            // let icon: cc.Sprite = cc.find(`bg/icon`, child).getComponent(cc.Sprite);
            // // label_level.string = `第${index + 1}关`;
            label_level.string = `${index + 1}`;
            // label_info.string = `${item_data.说明}`;

            // icon.spriteFrame = AssetsManager.图片[item_data.enemy_sprite];

            child.on(`click`, () => {
                this.click(index);
            }, this);
        }

        // this.click(0);
    }

    click(index) {
        MainPanel.Instance.ShowOff();

        // console.log(`index`, index);

        // GameData.CurrentLevelDataList = Fruit;
        GameData.CurrentLevel = index;

        cc.director.loadScene("GameScene");
    }

    AddListener() {

    }

    RemoveListener() {

    }

    init() {

    }
}